import time
import pymysql
import traceback


class G:
    usname: None


def get_time():
    time_str = time.strftime('%Y{}%m{}%d{} %X')
    return time_str.format("年", "月", "日")


def get_conn():
    conn = pymysql.connect(
        host="localhost",
        user="root",
        password="root",
        db="taskmanage",
        charset="utf8",
        port=3308,
    )
    cursor = conn.cursor()
    return conn, cursor


def close_conn(conn, cursor):
    if cursor:
        cursor.close()
    if conn:
        conn.close()


def query(sql, args):
    # 带参查询
    conn, cursor = get_conn()
    cursor.execute(sql, args)
    res = cursor.fetchall()
    close_conn(conn, cursor)
    return res


def query2(sql, *args):
    # 不带参查询
    conn, cursor = get_conn()
    cursor.execute(sql, args)
    res = cursor.fetchall()
    close_conn(conn, cursor)
    return res


def query3(sql, args):
    # 修改数据
    conn, cursor = get_conn()
    try:
        cursor.execute(sql, args)
        conn.commit()
        close_conn(conn, cursor)
        return "ok"
    except Exception as e:
        print("Happen some failed")

        return "failed"


def get_login(user, pwd):
    values = {"user": user, "pwd": pwd}
    sql = "select * from administrators where ad_name = %(user)s and ad_password = %(pwd)s"
    res = query(sql, values)
    gra = [i[3] for i in res]
    grade = ''.join(gra)
    name = [i[1] for i in res]
    G.usname = ''.join(name)
    return (len(res)), res


def get_adm():
    sql = "select * from administrators"
    res = query2(sql)
    return res


def add_adm(username, password, grade):
    values = {"username": username, "password": password, "grade": grade}
    sql = "INSERT INTO administrators(ad_name,ad_password,ad_grade)VALUES(%(username)s,%(password)s,%(grade)s)"
    res = query3(sql, values)
    return res


def dele_adm(username):
    values = {"username": username}
    sql = "DELETE FROM administrators WHERE ad_name=%(username)s"
    res = query3(sql, values)
    return res


def updt_adm(userID, username, password, grade):
    values = {"userID": userID, "username": username, "password": password, "grade": grade}
    sql = "UPDATE administrators SET ad_name = %(username)s , ad_password = %(password)s , ad_grade = %(grade)s WHERE ad_id=%(userID)s"
    res = query3(sql, values)
    return res


def updt_u(opassword, password):
    values = {"username": G.usname, "opassword": opassword, "password": password}
    sql = "UPDATE administrators SET ad_password = %(password)s WHERE ad_name = %(username)s AND ad_password = %(opassword)s"
    res = query3(sql, values)
    return res


# 创建任务
def task_add(name, describe, deadline):
    values = {"name": name, "describe": describe, "deadline": deadline}
    sql = "INSERT INTO task(name,description,create_time,deadline, status)VALUES(%(name)s,%(describe)s,NOW(),%(deadline)s,0)"
    res = query3(sql, values)
    return res


# 查看所有任务
def task_get():
    sql = "SELECT * FROM task"
    res = query2(sql)
    return res

# 根据id查询任务详情
def task_get_id(id):
    values = {"id": id}
    sql = "SELECT * FROM task WHERE id = %(id)s"
    res = query(sql, values)
    return res

# 编辑任务
def task_edit(id, name, describe, deadline, status):
    values = {"id": id, "name": name, "describe": describe, "deadline": deadline, "status": status}
    sql = "UPDATE task SET name = %(name)s, description = %(describe)s, deadline = %(deadline)s, status = %(status)s WHERE id = %(id)s"
    res = query3(sql, values)
    return res


# 根据id删除任务
def task_del(id):
    values = {"id": id}
    sql = "DELETE FROM task WHERE id = %(id)s"
    res = query3(sql, values)
    return res


if __name__ == '__main__':
    # print(get_login('root','aaa123456'))
    G.usname = "mmi"
    # print(eback_a('23','2','ax','123','xx'))
    # print(eback_c('10042','123'))
    # print(dback_ad('1','32435','霜立人烤箱','2','李四','223','hexi'))
    print(get_opass())
